<?php
namespace app\home\controller;
use think\Request;
use app\home\model\Article;
use think\facade\Session;
use app\home\model\History;
use app\home\model\Category;
use app\home\controller\Index;
class Index2 extends Index
{
    //热门文章
    public static function hot()
    {
        return Article::where('is_newest',1)->select();
    }

    //取浏览历史
    public static function history()
    {
        //没登陆状态
        if(!Session::get('userinfo')){
            $dataHistory = Session::get('history');
            krsort($dataHistory);
        }else{
            $dataHistory =  History::where('user_id',Session::get('userinfo')['id'])->limit(5)->order('browsing_time','desc')->all();
        }
        return $dataHistory ;
    }


    //主栏目
    public static function mainCategory()
    {
        return Category::where('p_id',0)->select();
    }

    //栏目关联的文章
    public static function column(Request $request)
    {
        $arrId = implode(',',Category::where('p_id',$request->get('cid'))->column('id'));
        return Article::where('category_id','in',$arrId.','.$request->get('cid'))->paginate(1,false,['query'=>request()->param()]);

    }

    //当前主栏目的所有子栏目
    public static function columnSon(Request $request)
    {
        return Category::where('p_id',$request->get('cid'))->select();
    }

}
